package br.com.foxinline.servico;

import br.com.foxinline.modelo.Saida;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author cadomeireles
 */
@Stateless
public class SaidaServico extends ServicoGenerico<Saida> {

    public SaidaServico() {
        super(Saida.class);
    }

    @Override
    public void deletar(Saida saida) {
        saida.setAtivo(false);
        super.atualizar(saida);
    }

    public List<Saida> pesquisar(Saida saida) {
        String sql = "select saida from Saida saida where saida.ativo = true";

        if (!saida.getDescricao().isEmpty()) {
            sql += " and lower(saida.descricao) like '%" + saida.getDescricao().toLowerCase() + "%'";
        }

        if (saida.getDataSaida() != null) {
            sql += " and saida.dataSaida = :data";
        }

        if (saida.getTipoSaida() != null) {
            sql += " and saida.tipoSaida = :tipoSaida";
        }

        Query query = getEntityManager().createQuery(sql);
        
        if (saida.getDataSaida() != null) {
            query.setParameter("data", saida.getDataSaida());
        }
        
        if (saida.getTipoSaida() != null) {
            query.setParameter("tipoSaida", saida.getTipoSaida());
        }

        return query.getResultList();
    }

}
